<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="创建时间" prop="createTimes">
        <el-date-picker
          v-model="queryParams.createTimes"
          type="daterange"
          style="width: 220px"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="timestamp"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="车辆编号" prop="carNumber">
        <el-input
          v-model.trim="queryParams.carNumber"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carLicensePlate" label-width="58px">
        <el-input
          v-model.trim="queryParams.carLicensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dispatch:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="dispatchList"
      max-height="420"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        min-width="160"
      >
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="外派开始时间"
        align="center"
        prop="beginDate"
        width="140"
      />
      <el-table-column
        label="外派结束时间"
        align="center"
        prop="endDate"
        width="140"
      />
      <el-table-column
        label="外派状态"
        align="center"
        prop="dispatchState"
        width="140"
      />
      <el-table-column
        label="外派站点"
        align="center"
        prop="site"
        width="120"
      />
      <el-table-column
        label="联系人"
        align="center"
        prop="associates"
        width="120"
      />
      <el-table-column
        label="联系人电话"
        align="center"
        prop="phoneNumber"
        width="180"
      />
      <el-table-column label="备注" align="center" prop="note" width="180" :show-overflow-tooltip="true"/>
      <el-table-column
        label="操作"
        align="center"
        width="220"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native="stopDispatch(scope.row)"
            >终止外派</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改外派管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="外派开始时间" prop="startTime">
          <el-input
            v-model="queryParams.startTime"
            placeholder="请输入外派开始时间"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="外派结束时间" prop="endTime">
          <el-input
            v-model="queryParams.endTime"
            placeholder="请输入外派结束时间"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="外派状态" prop="dispatchState">
          <el-input
            v-model="queryParams.dispatchState"
            placeholder="请输入外派状态"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="外派站点" prop="site">
          <el-input
            v-model="queryParams.site"
            placeholder="请输入外派站点"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="associates">
          <el-input
            v-model="queryParams.associates"
            placeholder="请输入联系人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系人电话" prop="phoneNumber">
          <el-input
            v-model="queryParams.phoneNumber"
            placeholder="请输入联系人电话"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="0:未删除/1:已删除" prop="isDeleted">
          <el-input
            v-model="queryParams.isDeleted"
            placeholder="请输入0:未删除/1:已删除"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDispatch,
  getDispatch,
  delDispatch,
  addDispatch,
  updateDispatch,
  updateStatus,
} from "@/api/system/dispatch";
import { formatDate } from "@/utils";
export default {
  name: "Dispatch",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外派管理表格数据
      dispatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTimes: null,
        carNumber: null,
        carLicensePlate: null,
      },
      timer: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [
          { required: true, message: "外派开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "外派结束时间不能为空", trigger: "blur" },
        ],
        dispatchState: [
          { required: true, message: "外派状态不能为空", trigger: "blur" },
        ],
        site: [
          { required: true, message: "外派站点不能为空", trigger: "blur" },
        ],
        associates: [
          { required: true, message: "联系人不能为空", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "联系人电话不能为空", trigger: "blur" },
        ],
        note: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        isDeleted: [
          {
            required: true,
            message: "0:未删除/1:已删除不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.timer = setInterval(() => {
      this.getList();
    }, 5000);
  },
  activated() {
    this.getList();
  },

  deactivated() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    formatDate,
    /** 查询外派管理列表 */
    getList() {
      if (!this.timer) {
        this.loading = true;
      }

      listDispatch({
        ...this.queryParams,
        createTimeStart: this.queryParams?.createTimes?.[0],
        createTimeEnd: this.queryParams?.createTimes?.[1],
      }).then((response) => {
        this.dispatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        startTime: null,
        endTime: null,
        dispatchState: null,
        site: null,
        associates: null,
        phoneNumber: null,
        note: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDeleted: null,
      };
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      //   this.open = true;
      //   this.title = "添加外派管理";
      this.$router.push({
        path: "/logistics/dispatchAction",
      });
    },
    stopDispatch(row) {
      console.log(row);
      if (!(row.dispatchState === "待开始")) {
        return this.$message.warning("仅限于待开始的状态可修改");
      }
      updateStatus({ ...row }).then((res) => {
        if (res.code === 200) {
          this.$modal.msgSuccess("修改成功");
          handleQuery();
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.$router.push({
        path: `/logistics/dispatchAction?id=${id}`,
      });
      //   getDispatch(id).then((response) => {
      //     this.form = response.data;
      //     this.open = true;
      //     this.title = "修改外派管理";
      //   });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDispatch(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDispatch(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('确认删除吗？')
        .then(function () {
          return delDispatch(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dispatch/export",
        {
          ...this.queryParams,
        },
        `dispatch_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
